﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace ASPMVC_first_app.Models
{
    public class Min18YearsForMembershipTypes:ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var customer = (Customer)validationContext.ObjectInstance;

            if (customer.MembershipTypeId == MembershipType.Unknown || customer.MembershipTypeId == MembershipType.Free)
                return ValidationResult.Success;

            if (customer.DateOfBirth== null)
                return new ValidationResult("Please provide Date of Birth");

            var age = DateTime.Now.Year - customer.DateOfBirth.Value.Year;

            return(age >= 18) 
                ? ValidationResult.Success 
                : new ValidationResult("You should be at least 18 year old");
        }
    }
}